package com.fg.cloud.wechar.controller.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.CategoryTree;
import com.fg.cloud.common.finals.Agency;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.product.ProductInfoEntity;
import com.fg.cloud.manager.entry.product.vo.AgencyParmsVo;
import com.fg.cloud.manager.entry.product.vo.ProductInfoVo;
import com.fg.cloud.manager.service.product.AgencyProductHomeService;
import com.fg.cloud.manager.service.product.ProductCategoryService;
import com.fg.cloud.wechar.controller.BaseWeCharController;

/**
 * 
 * Description:经销商商品部分
 * @author 陈银龙
 * @date 2017年8月22日下午4:03:48
 */
@Controller
@RequestMapping("/weCharProductHome")
public class WeCharProductHomeController extends BaseWeCharController{

	@Autowired
	private AgencyProductHomeService agencyProductHomeService; 
	
	@Autowired
	private ProductCategoryService categoryInterface ;
	
	/**
	 * Description:查询经销商能见得所有分类
	 * @param req
	 * @param resp
	 * @param facId
	 * @author 陈银龙
	 * @date 2017年8月21日下午1:59:09
	 */
	@RequestMapping(value = "/findAllCategory",method = RequestMethod.POST)
	@ResponseBody
	public void findAllCategory(HttpServletRequest req , HttpServletResponse resp){
		try {
			AgencyUserVo user  = getCurrentAgencyUser();
			Map<String , Object> usmap = new HashMap<String ,Object>();
			usmap.put("facId", user.getFacId());
			usmap.put("ageId", user.getAgeId());
			usmap.put("tableName", Agency.getTableName(user.getFacId()));

			List<CategoryTree> tree = categoryInterface.findAllCategory(usmap);
			if (tree != null) {
				doResult(resp, true, StatusCode.FINDSUCCESS, tree, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, tree, ResultType.FIND_NULL);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * Description:高级查询
	 * @param req
	 * @param resp
	 * @param ap
	 * @author 陈银龙
	 * @date 2017年8月21日下午7:18:03
	 */
	@RequestMapping(value = "/findBySenior",method = RequestMethod.POST)
	@ResponseBody
	public void findBySenior(HttpServletRequest req , HttpServletResponse resp,AgencyParmsVo ap){
		try{
			if (ap.getPageNum() == null || ap.getPageNum() < 0 || ap.getPageSize() == null || ap.getPageSize() < 0) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return;
			}
			PageUtils<ProductInfoVo> pageinfo = new PageUtils<ProductInfoVo>(ap.getPageNum(), ap.getPageSize(),ap.getSort(),ap.getOrder());
			Map<String, Object> condition = pageinfo.getCondition();
			AgencyUserVo user = getCurrentAgencyUser();
			//FactoryUserVo user = getCurrentFactoryUser();
			
			condition.put("facId", user.getFacId());
			condition.put("ageId", user.getAgeId());
			condition.put("tableName", Agency.getTableName(user.getFacId()));
			condition.put("pageNum", ap.getPageNum());
			condition.put("pageSize", ap.getPageSize());
			
			condition.put("id", ap.getId());
			condition.put("isNewProduct", ap.getIsNewProduct());
			condition.put("isPerformence", ap.getIsPerformence());
			condition.put("isConmodity", ap.getIsConmodity());
			condition.put("stock", ap.getStock());// 库存
			condition.put("categoryNumber", ap.getCategoryNumber()+"_"+ap.getCategoryId());//商品分类number
			condition.put("categoryId", ap.getCategoryId());//商品id
			condition.put("productName", ap.getProductName());//商品名
			condition.put("productNumber", ap.getProductNumber());//商品编码
			condition.put("barcode", ap.getBarcode());// 商品条码
			condition.put("collection", ap.getCollection());// 商品收藏
			
			condition.put("buy", ap.getBuy());// 订过的商品
			condition.put("keyword", ap.getKeyword());//关键字:包含了商品编码,规则,名称
			agencyProductHomeService.findBySenior(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询前三层数据并组成树形结构
	 * @param req
	 * @param resp
	 * @param facid
	 * @author 陈银龙
	 * @date 2017年8月9日下午2:41:33
	 */
	@RequestMapping(value="/selectThreeByFacId",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectThreeByFacId(HttpServletRequest req , HttpServletResponse resp){
		try {
			AgencyUserVo user  = getCurrentAgencyUser();
			Map<String , Object> usmap = new HashMap<String ,Object>();
			usmap.put("facId", user.getFacId());
			usmap.put("ageId", user.getAgeId());
			usmap.put("tableName", Agency.getTableName(user.getFacId()));

			List<CategoryTree> tree = categoryInterface.selectThreeByFacId(usmap);
			if (tree != null) {
				doResult(resp, true, StatusCode.FINDSUCCESS, tree, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, tree, ResultType.FIND_NULL);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}

	/**
	 * Description:查找我定过的
	 * @param req
	 * @param resp
	 * @param ap
	 * @author 陈银龙
	 * @date 2017年9月4日上午11:09:40
	 */
	@RequestMapping(value = "/findBuy",method = RequestMethod.POST)
	@ResponseBody
	public void findBuy(HttpServletRequest req , HttpServletResponse resp, Integer pageNum, Integer pageSize
			,String order, String sort){
		try{
			if (pageNum == null || pageNum < 0 || pageSize == null ||pageSize < 0) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return;
			}
			PageUtils<ProductInfoEntity> pageinfo = new PageUtils<ProductInfoEntity>(pageNum, pageSize ,sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			
			AgencyUserVo user = getCurrentAgencyUser();
			condition.put("facId", user.getFacId());
			condition.put("ageId", user.getAgeId());
			agencyProductHomeService.findBuy(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:手机端高级搜索
	 * @param req
	 * @param resp
	 * @param ap
	 * @author 陈银龙
	 * @date 2017年9月4日上午11:09:40
	 */
	@RequestMapping(value = "/findWeCharBySenior",method = RequestMethod.POST)
	@ResponseBody
	public void findWeCharBySenior(HttpServletRequest req , HttpServletResponse resp,AgencyParmsVo ap){
		try{
			if (ap.getPageNum() == null || ap.getPageNum() < 0 || ap.getPageSize() == null || ap.getPageSize() < 0) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return;
			}
			PageUtils<ProductInfoVo> pageinfo = new PageUtils<ProductInfoVo>(ap.getPageNum(), ap.getPageSize(),ap.getSort(),ap.getOrder());
			Map<String, Object> condition = pageinfo.getCondition();
			AgencyUserVo user = getCurrentAgencyUser();
			//FactoryUserVo user = getCurrentFactoryUser();
			
			condition.put("facId", user.getFacId());
			condition.put("ageId", user.getAgeId());
			condition.put("tableName", Agency.getTableName(user.getFacId()));
			condition.put("pageNum", ap.getPageNum());
			condition.put("pageSize", ap.getPageSize());
			
			condition.put("isNewProduct", ap.getIsNewProduct());
			condition.put("isPerformence", ap.getIsPerformence());
			condition.put("isConmodity", ap.getIsConmodity());
			condition.put("stock", ap.getStock());// 库存
			condition.put("keyword", ap.getKeyword());//商品名
			condition.put("collection", ap.getCollection());// 收藏过的商品
			condition.put("buy", ap.getBuy());// 订过的商品
			condition.put("categoryNumber", ap.getCategoryNumber()+"_"+ap.getCategoryId());//商品分类number
			condition.put("categoryId", ap.getCategoryId());//商品id
			
			agencyProductHomeService.findWeCharBySenior(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:商品规格和详情图片
	 * @param req
	 * @param resp
	 * @param ap
	 * @author 陈银龙
	 * @date 2017年9月4日上午8:44:40
	 */
	@RequestMapping(value = "/findParamAndImg",method = RequestMethod.POST)
	@ResponseBody
	public void findParamAndImg(HttpServletRequest req , HttpServletResponse resp,Long productId){
		try{
			if(productId == null || productId < 0){
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId", user.getFacId());
			usmap.put("ageId", user.getAgeId());
			usmap.put("tableName", Agency.getTableName(user.getFacId()));
			usmap.put("productId", productId);
			ProductInfoVo psv = agencyProductHomeService.findParamAndImg(usmap);
			doResult(resp, true, StatusCode.FINDSUCCESS, psv, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
}
